<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Validate {

	protected $_input = false;
	protected $_rule = array();

	protected $_result = true;
	protected $_message = '';
	protected $_items = array();

	protected $_CI;

	public function __construct(){
		$this->_CI = & get_instance();
	}

	public function setInput($input){
		$this->_input = $input;
	}

	public function setRule($key,$rule,$value = true){
		$this->_rule[$key][$rule] = $value;
		if(is_string($value)) {
			$this->_rule[$key]['message'] = $value;
		}
	}

	public function run($input = false){
		if($input !== false) $this->_input = $input;
		$res = array();
		foreach($this->_rule as $key => $rules){

			//init rules
			if(!isset($rules['message'])) $rules['message'] = $this->_CI->lang->line('error_parameter');
			if(!isset($rules['optional'])) $rules['optional'] = false;
			if(!isset($rules['notnull'])) $rules['notnull'] = true;
			if(isset($rules['file']) && isset($_FILES[$key])){
				$this->_input[$key] = $_FILES[$key];
			}

			//no target?wrong or optional
			if(!isset($this->_input[$key])){
				if($rules['optional']){
					continue;
				}else{
					$this->_result = false;
					$this->_message = (ENVIRONMENT != 'production' ? $key : '').$rules['message'];
					break;
				}
			}

			//not null
			if($rules['notnull'] && !$this->validate($this->_input[$key])){
				$this->_result = false;
				$this->_message = (ENVIRONMENT != 'production' ? $key : '').$rules['message'];
				break;
			}

			//array
			if(isset($rules['array']) && !is_array($this->_input[$key])){
				$this->_result = false;
				$this->_message = (ENVIRONMENT != 'production' ? $key : '').$rules['message'];
				break;
			}

			//file
			if(isset($rules['file']) && $this->_input[$key]['error'] != 0){
				if($this->_input[$key]['error'] == 4 && $rules['optional']){
					continue;
				}else{
					$this->_result = false;
					$this->_message = (ENVIRONMENT != 'production' ? $key : '').$rules['message'];
					break;
				}
			}

			if(isset($rules['integer'])) $res[$key] = intval($this->_input[$key]);
			if(isset($rules['string'])) $res[$key] = strip_tags(trim(strval($this->_input[$key])));
			if(isset($rules['editor'])) $res[$key] = trim(strval($this->_input[$key]));
			if(isset($rules['float'])) $res[$key] = floatval($this->_input[$key]);
			if(isset($rules['array'])) $res[$key] = $this->_input[$key];
			if(isset($rules['file'])) $res[$key] = $this->_input[$key];
		}

		$this->_items = $res;
		return $this->_result;
	}

	public function isSuccess(){
		return $this->_result;
	}

	public function getMessage(){
		return $this->_message;
	}

	public function getResult(){
		return $this->_items;
	}

	public function reset(){
		$this->_input = false;
		$this->_rule = array();
	}

	public function validate($input,$min = 1,$max = -1){

		if(is_array($input)){
			return !empty($input);
		}

		$len = strlen($input);
		$res = true;
		if($min != -1 && $len < $min){
			$res = false;
		}
		if($max != -1 && $len > $max){
			$res = false;
		}

		return $res;
	}
}

/* End of file validate.php */
/* Location: ./application/libraries/validate.php */